# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/usr/bin/env sh

set -e

PROMETHEUS_VERSION=${PROMETHEUS_VERSION:-v2.53.3}
ALERTMANAGER_VERSION=${ALERTMANAGER_VERSION:-v0.27.0}
GRAFANA_VERSION=${GRAFANA_VERSION:-11.4.0}
PROMETHEUSALERT_VERSION=${PROMETHEUSALERT_VERSION:-v4.9.1}
ip=$(hostname -I | awk '{print $1}')
export IP=${ip}
usage() {

  echo "Usage: quickstart-docker.sh [-f]"
  echo "  -f  Run in the foreground"
  exit 1
}

while getopts "hf" opt; do
  case $opt in
    f)
      DETACHED=false
      ;;
    h)
      usage
      exit 0
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
  esac
done

echo "本机IP地址: $ip"
echo "Checking if Docker is installed..."
if ! [ -x "$(command -v docker)" ]; then
  echo "Docker is not found. Please make sure Docker is installed and the docker command is available in PATH."
  exit 1
fi

echo "Checking if Docker Compose is installed..."
if ! [ -x "$(command -v docker-compose)" ]; then
  echo "Docker Compose is not found. Please make sure Docker Compose is installed and the docker-compose command is available in PATH."
  exit 1
fi


export PROMETHEUS_IMAGE=prom/prometheus:${PROMETHEUS_VERSION}
export ALERTMANAGER_IMAGE=prom/alertmanager:${ALERTMANAGER_VERSION}
export GRAFANA_IMAGE=grafana/grafana:${GRAFANA_VERSION}
export PROMETHEUSALERT_IMAGE=feiyu563/prometheus-alert:${PROMETHEUSALERT_VERSION}
export IP=${ip}

echo "Installing Prometheus ${PROMETHEUS_VERSION} with ${ALERTMANAGER_VERSION} Alertmanager and ${GRAFANA_VERSION} Grafana..."

docker compose -f "./prometheus/docker-compose.yml" \
  --project-name=prometheus-quickstart \
  up \
  --detach=${DETACHED:-true} \
  --wait

echo "Prometheus is now running. You can see management platform to localhost:9090 and access the UI at http://localhost:9090."
echo "Alertmanager is now running. You can see management platform to localhost:9093 and access the UI at http://localhost:9093."
echo "Grafana is now running. You can see management platform to localhost:3000 and access the UI at http://localhost:3000."
echo "To find Prometheus Docs, follow the link to our documentation site https://prometheus.io/docs/introduction/overview/."
echo "To find Grafana Docs, follow the link to our documentation site https://grafana.com/docs/grafana/latest/setup-grafana/installation/docker/."
echo "To stop Prometheus, run 'docker compose --project-name=prometheus-quickstart down'."